﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CNCSoft.EData;
using CNCSoft.EData.SqlServer;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;

using Common;

namespace CNCSoft.ErpApps.TMS.Vattu
{
    public partial class frmThemVatTu : Form
    {
        public frmThemVatTu()
        {
            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

            InitializeComponent();
        }


    #region [ Attributes ]
        private string _MaVatTu;
        private string _TenVatTu;
        private string _TenVatTuCha;
        private bool _addNewMode;

        public string MaVatTu
        {
            get { return _MaVatTu; }
            set { _MaVatTu = value; }
        }

        public string TenVatTu
        {
            get { return _TenVatTu; }
            set { _TenVatTu = value; }
        }

        public string TenVatTuCha
        {
            get { return _TenVatTuCha; }
            set { _TenVatTuCha = value; }
        }


        public bool addNewMode
        {
            get { return _addNewMode; }
            set { _addNewMode = value; }
        }

    #endregion


        private void frmThemVatTu_Load(object sender, EventArgs e)
        {
            //Load DM_DVT len combo cboDonViTinh
            //this.cboDVT.DataBindings.Add("SelectedValue", InvoiceData.getDataSet(), "Invoice.Certificate");
            DataTable dtDVT = new DataTable();
            SQLHelper.Fill(dtDVT, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_DONVITINH");
            this.cboDVT.DataSource = dtDVT;
            this.cboDVT.DisplayMember = "TenDonViTinh";
            this.cboDVT.ValueMember = "MaDonViTinh"; 

            txtMaVatTu.Text = _MaVatTu;
            //txtTenVatTu.Text = _TenVatTu;

            if (_addNewMode && _TenVatTuCha != "")
            {
                toolStripStatusLabel1.Text = "Thêm vật tư mới trong mục: " + _TenVatTuCha;
                txtDonGia.Text = "0";
            }
            else
                toolStripStatusLabel1.Text = "";

        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                _MaVatTu = txtMaVatTu.Text;
                _TenVatTu = txtTenVatTu.Text;

                //Luu vao table DM_VATTU
                string SQLInsert = "";
                if (_addNewMode)
                    SQLInsert = "INSERT INTO DM_VATTU (MaVattu, TenVattu, DVT, DonGia) VALUES ('" + _MaVatTu + "', N'" + _TenVatTu.Trim() +  "', '" + cboDVT.SelectedValue +  "', " +  txtDonGia.Text + " )";
                else  //Update
                    SQLInsert = "UPDATE DM_VATTU Set TenVattu = N'" + _TenVatTu.Trim() + "', DVT = '" + cboDVT.SelectedValue + "', DonGia = " +  txtDonGia.Text + " WHERE MaVatTu = '" + _MaVatTu.Trim() + "'";
                //MessageBox.Show(SQLInsert);
                SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString, SQLInsert);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void txtMaVatTu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                txtTenVatTu.Text = CNCSoft.EData.SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                                    "SELECT TenVatTu FROM DM_VATTU WHERE MaVatTu = '" + txtMaVatTu.Text + "'").ToString();
                
                cboDVT.SelectedValue = CNCSoft.EData.SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                                    "SELECT DVT FROM DM_VATTU WHERE MaVatTu = '" + txtMaVatTu.Text + "'").ToString();
                
                txtDonGia.Text = CNCSoft.EData.SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                            "SELECT isnull(DonGia,0) FROM DM_VATTU WHERE MaVatTu = '" + txtMaVatTu.Text + "'").ToString();
            }
            catch
            {
            }

        }
    }
}
